//
//  StyleView.m
//  JU53
//
//  Created by RoyceHe on 15/12/9.
//  Copyright © 2015年 liuhengping. All rights reserved.
//

#import "StyleView.h"
@interface StyleView()
@property (nonatomic, copy) SKUClick block;

@end
@implementation StyleView

//限制最大宽度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
//不限制最大宽度
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font
{
    return [self sizeWithText:text font:font maxW:MAXFLOAT];
}

+ (instancetype)styleViewWith:(NSArray *)typeArr row:(CGFloat)row{
    StyleView *style = [[StyleView alloc]init];
   
    UILabel *color = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 80, 20)];
    color.textColor = [UIColor blackColor];
    color.font = [UIFont systemFontOfSize:12];
    [style addSubview:color];
   
    CGFloat maxW = kWidth - 8;
    
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    CGFloat h = 35;//用来控制button距离父视图的高
    for (int i = 0; i < typeArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        button.tag = 100 * row + i;
      // button.backgroundColor = DefaultBGGray;
		button.layer.cornerRadius = 3;
        button.layer.borderColor = [UIColor colorWithRed:216.0/255 green:218.0/255 blue:219.0/255 alpha:1].CGColor;
        button.layer.borderWidth = 1;
		button.clipsToBounds = YES;
		[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
       // [button addTarget:self action:click forControlEvents:(UIControlEventTouchUpInside)];
        //根据计算文字的大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13]};
        CGFloat length = [typeArr[i] boundingRectWithSize:CGSizeMake(maxW, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        //为button赋值
        [button setTitle:typeArr[i] forState:UIControlStateNormal];
        //设置button的frame
        button.frame = CGRectMake(10 + w, h , length + 35 , 30);
        //当button的位置超出屏幕边缘时换行
        if(10 + w + length + 15 > maxW){
            w = 0; //换行时将w置为0
            h = h + button.frame.size.height + 10;//距离父视图也变化
            button.frame = CGRectMake(10 + w, h, length + 20, 30);//重设button的frame
        }
        w = button.frame.size.width + button.frame.origin.x;
        [style addSubview:button];
        if (i == typeArr.count - 1) {
            style.frame = CGRectMake(0, 0, kWidth, button.frame.size.height + button.frame.origin.y + 10);
        }
        
        
    }
    
    return style;
}
- (void)SKUBtnClick:(SKUClick)block {
    
	self.block = block;

}
/**按钮点击方法*/
//- (void)handleClick:(UIButton *)sender{
//
////	if (self.block) {
////		self.block(sender.tag - 100);
////	}
//
//
//}
@end
